package com.pronhub.cloud.shop.service.impl;

import com.pronhub.cloud.shop.domain.SeckillOrder;
import com.pronhub.cloud.shop.mapper.SeckillOrderMapper;
import com.pronhub.cloud.shop.redis.MyRedisTemplate;
import com.pronhub.cloud.shop.redis.SeckillKeyPrefix;
import com.pronhub.cloud.shop.service.ISeckillOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SeckillOrderServiceImpl implements ISeckillOrderService {

    @Autowired
    private SeckillOrderMapper seckillOrderMapper;

    @Autowired
    private MyRedisTemplate myRedisTemplate;

    @Override
    public SeckillOrder findByUserIdAndSeckillId(Long userId, Long seckillId) {
        return seckillOrderMapper.findByUserIdAndSeckillId(userId,seckillId);
    }

    @Override
    public void createSeckillOrder(Long userId, Long seckillId, String orderNo) {
        SeckillOrder seckillOrder = new SeckillOrder();
        seckillOrder.setOrderNo(orderNo);//订单编号
        seckillOrder.setSeckillId(seckillId);//秒杀场次id
        seckillOrder.setUserId(userId);//用户id
        seckillOrderMapper.insert(seckillOrder);
        //往Redis里面添加一个购买记录数据 就是在Redis中设计相关的东西 就是需要在后面快速的查询 尽量少的操作MySql
        myRedisTemplate.set(SeckillKeyPrefix.SECKILL_ORDER,seckillId+":"+userId,true);
    }
}
